`
https://leetcode.cn/problems/find-the-longest-equal-subarray/
`

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var longestEqualSubarray = function (nums, k) {
  // 分组查找，把相同的数分到同一组，记录下标，计算每一组的最长结果即可
  const posList = new Map()
  for (const i in nums) {
    const num = nums[i]
    if (!posList.has(num)) {
      posList.set(num, [])
    }
    const pos = posList.get(num)
    pos.push(i)
  }
  let res = 0
  for (const pos of posList.values()) {
    const n = pos.length
    let cost = 0
    let left = 0, right = 0

    while (right < n) {
      const i1 = pos[right++]
      cost = i1 - pos[left] + 1 - (right - left)

      while (cost > k) {
        left++
        cost = i1 - pos[left] + 1 - (right - left)
      }

      res = Math.max(res, right - left)
    }
  }

  return res
};